package com.idsmanager.zdh.service.dto.api;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.idsmanager.oauth.domain.security.Privilege;
import com.idsmanager.oauth.domain.security.User;
import com.idsmanager.oauth.service.dto.user.UserDto;
import com.idsmanager.oidc.rs.result.APIResult;
import com.idsmanager.zdh.infrastructure.APIError;
import org.springframework.validation.ObjectError;

import java.util.List;
import java.util.Set;

public class UserApiResult extends APIResult {

    private static final long serialVersionUID = 6452645568767153393L;

    private String uuid;

    private String username;

    private Set<Privilege> privileges;

    public UserApiResult() {
    }

    public UserApiResult(APIError apiError) {
        this(apiError, apiError.getDescription());
    }

    public UserApiResult(APIError apiError, String error) {
        this.statusCode = apiError.getErrorNumber();
        this.errors.add(error);
    }

    public UserApiResult(APIError apiError, List<ObjectError> errors) {
        this(apiError);
        if (errors.isEmpty()) {
            this.errors.add(apiError.getDescription());
        } else {
            for (ObjectError error : errors) {
                this.errors.add(error.getDefaultMessage());
            }
        }
    }

    public UserApiResult(User user) {
        this.uuid = user.uuid();
        this.username = user.username();
        this.privileges = user.privileges();
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Set<Privilege> getPrivileges() {
        return privileges;
    }

    public void setPrivileges(Set<Privilege> privileges) {
        this.privileges = privileges;
    }
}
